/**
 * jims
 */
package com.jims.master.modules.drug.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.drug.entity.DrugImportMaster;
import com.jims.master.modules.drug.service.DrugImportMasterService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 入库主记录Controller
 * @author zhuq
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugImportMaster")
public class DrugImportMasterController extends BaseController {

	@Autowired
	private DrugImportMasterService drugImportMasterService;
	
	@ModelAttribute
	public DrugImportMaster get(@RequestParam(required=false) String id) {
		DrugImportMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugImportMasterService.get(id);
		}
		if (entity == null){
			entity = new DrugImportMaster();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugImportMaster drugImportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugImportMaster> page = drugImportMasterService.findPage(new Page<DrugImportMaster>(request, response), drugImportMaster); 
		model.addAttribute("page", page);
		return "modules/drug/drugImportMasterIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugImportMaster drugImportMaster, Model model) {
		model.addAttribute("drugImportMaster", drugImportMaster);
		return "modules/drug/drugImportMasterForm";
	}
	/**
	 * 入库记录查询
	 * @param drugImportMaster
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"query", ""})
	public String listque(DrugImportMaster drugImportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugImportMaster> page = drugImportMasterService.findImportList(new Page<DrugImportMaster>(request, response), drugImportMaster);
		Date date = drugImportMaster.getCreateDate();
		Date date1 = drugImportMaster.getUpdateDate();

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (date != null) {
			drugImportMaster.setCreateDateStr(dateformat.format(date));
		}
		if (date1 != null) {
			drugImportMaster.setUpdateDateStr(dateformat.format(date1));
		}
		model.addAttribute("drugImportMaster",drugImportMaster);
		model.addAttribute("page", page);
		return "modules/drug/drugImportQueryIndex ";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugImportMaster drugImportMaster, Model model) {
	    StringData data=new StringData();
		int i=drugImportMasterService.save(drugImportMaster);
        if(i>0){
            data.setCode("success");
            data.setData("保存入库成功");
        }else{
            data.setCode("error");
            data.setData("保存入库失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugImportMaster drugImportMaster) {
	    StringData data=new StringData();
		int i=drugImportMasterService.delete(drugImportMaster);
        if(i>0){
            data.setCode("success");
            data.setData("删除入库成功");
        }else{
            data.setCode("error");
            data.setData("删除入库失败");
        }
		return data;
	}

}